//
//  HTTPTool.m
//  HeartTravel
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HTTPTool.h"



@implementation HTTPTool


+ (void)get:(NSString *)url
       body:(id)body
 httpResult:(HTTPStyle)style
    success:(void(^)(id result))success
    failure:(void(^)(NSError *error))failure
{
    
    //获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //判断返回的responseObject的数据类型
    switch (style) {
            //NSData
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            //JSON
        case JSON:
            break;
            //XML
        case XML:manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        default:
            break;
    }
    //让管理类调用get请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //如果成功返回数据
        if (success) {
            
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //如果失败返回error信息
        if (failure) {
            failure(error);
        }
        
        
    }];
}


+ (void)post:(NSString *)url
        body:(id)body
 httpResult:(HTTPStyle)style
requestBodyStyle:(RequestBodyStyle)bodyStyle
     success:(void(^)(id result))success
     failure:(void(^)(NSError *error))failure
{
    //获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //判断返回的responseObject的数据类型
    switch (style) {
            //NSData
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            //JSON
        case JSON:
            break;
            //XML
        case XML:manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        default:
            break;
    }
    
    
    //判断post请求body的类型
    switch (bodyStyle) {
        case BodyHTTP:
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
        case BodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        default:
            break;
    }
    
    //让管理类调用post请求
    [manager POST:body parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //如果成功返回数据
        if (success) {
            
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        //如果失败返回error信息
        if (failure) {
            failure(error);
        }
    }];
}

@end
